package com.mdd.common.config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shenjian
 * @createTime 2023年02月11日 10:24:00
 */
@Data
@Configuration
@ConfigurationProperties(prefix = WxPayConfig.PREFIX)
public class WxPayConfig {

    public static final String PREFIX = "wx-pay";

    /**
     * 商户号
     */
    private String merchantId;

    /**
     * 商户API私钥路径
     */
    private String privateKeyPath;

    /**
     * 商户API证书序列号
     */
    private String merchantSerialNumber;

    /**
     * APIv3密钥
     */
    private String apiV3Key;

    /**
     * APPID
     */
    private String appId;

    /**
     * appSecret
     */
    private String appSecret;

    /**
     * 微信服务器地址
     */
    private String domain = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";

    /**
     * 接收结果通知地址
     */
    private String payNotifyDomain;

    /**
     * 退款结果通知地址
     */
    private String refundNotifyDomain;

    /**
     * 微信的证书配置
     *
     * @return RSAAutoCertificateConfig
     */
    @Bean("wxCertificateConfig")
    public RSAAutoCertificateConfig getWxCertificateConfig() {
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
    }

    /**
     * JsApi服务类
     *
     * @return JsapiService
     */
    @Bean("wxJsapiService")
    public JsapiService wxJsapiService() {
        return new JsapiService.Builder()
                .config(this.getWxCertificateConfig())
                .build();
    }

    /**
     * Refund服务类
     *
     * @return JsapiService
     */
    @Bean("refundService")
    public RefundService refundService() {
        return new RefundService.Builder()
                .config(this.getWxCertificateConfig())
                .build();
    }

    /**
     * 生成前端调用支付参数
     *
     * @return jsapiServiceExtension
     */
    @Bean("jsapiServiceExtension")
    public JsapiServiceExtension jsapiServiceExtension() {
        return new JsapiServiceExtension.Builder()
                .config(this.getWxCertificateConfig())
                .build();
    }
}

